
package com.kaciula.letsdoit.ui;

import com.kaciula.letsdoit.R;
import com.kaciula.letsdoit.db.WastesDbAdapter;
import com.kaciula.letsdoit.service.LdirWebService;

import android.app.Activity;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class UploadActivity extends Activity implements OnClickListener {

    private String mUsername;

    private String mMd5Pass;

    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload);

        PreferenceHelper prefHelper = ((MyApplication) getApplicationContext()).getPrefHelper();

        if (prefHelper.isCredentialsOk()) {
            mUsername = prefHelper.getUsername();
            mMd5Pass = prefHelper.getMd5Pass();
        }

        Button upload = (Button) findViewById(R.id.upload);
        upload.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            long id = extras.getLong(WastesDbAdapter.KEY_ROWID);
            upload.setText(getString(R.string.upload) + " " + getString(R.string.waste) + " " + id);
        }

        mProgress = (ProgressBar) findViewById(R.id.progress);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.upload:
                long id = -1;
                Bundle extras = getIntent().getExtras();

                if (extras != null) {
                    id = extras.getLong(WastesDbAdapter.KEY_ROWID);
                }

                new UploadTask(id).execute();
                break;
            default:
                break;
        }
    }

    private class UploadTask extends AsyncTask<Void, Integer, Boolean> {

        private WastesDbAdapter mDbHelper;

        private Cursor mWastesCursor;

        private int mMax;

        public UploadTask() {
            mDbHelper = new WastesDbAdapter(UploadActivity.this);
            mDbHelper.open();
        }

        public UploadTask(long id) {
            this();

            if (id > 0) {
                mWastesCursor = mDbHelper.fetchWaste(id);
            } else {
                mWastesCursor = mDbHelper.fetchAllWastes();
            }

            mMax = mWastesCursor.getCount();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgress.setMax(mMax + 1);
            mProgress.setProgress(1);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            LdirWebService service = new LdirWebService(UploadActivity.this);
            long id;
            int i = 1;
            boolean isSuccessful = true;

            if (mMax > 0) {
                try {
                    mWastesCursor.moveToFirst();
                    do {
                        id = mWastesCursor.getLong(mWastesCursor
                                .getColumnIndexOrThrow(WastesDbAdapter.KEY_ROWID));
                        service.uploadWaste(id, mUsername, mMd5Pass, false);
                        publishProgress(1);
                        i++;
                    } while (mWastesCursor.moveToNext());
                } catch (Throwable e) {
                    isSuccessful = false;
                }
            }

            mWastesCursor.close();
            mDbHelper.close();

            return isSuccessful;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            mProgress.incrementProgressBy(values[0]);
        }

        @Override
        protected void onPostExecute(Boolean uploadSuccessful) {
            if (uploadSuccessful) {
                Toast.makeText(UploadActivity.this, R.string.upload_successful, Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(UploadActivity.this, R.string.upload_failed, Toast.LENGTH_SHORT)
                        .show();
            }

            setResult(Activity.RESULT_OK);
            finish();
        }

    }
}
